<?php

class Ebx_Helper_ImageResize implements Ebx_Helper_Interface {

	protected $_options = array();

	protected $_file = null;

	protected $_resourceType = '';

	protected $_resource = null;

	function __construct($options = null)
    {
    	if ($options !== null) {
			$this->setOptions($options);
		}
    }

	function setOptions($options) {
		$this->_options = $options;
	}

	function getOptions() {
		if (isset($this->_options['options'])) {
			return $this->_options['options'];
		}
		return $this->_options;
	}

	function direct($value, $options = array()) {
		$path = $value;
		$options = $this->getOptions();

		$this->_file = new SplFileObject($path);
		$this->_resource = null;

		$w = $this->getWidth();
		$h = $this->getHeight();

		if ($options['type'] == 'zoom') {
			if ($w > $options['width']) {
				$this->zoom('w', $options['width']);
			} elseif ($h > $options['height']) {
				$this->zoom('h', $options['height']);
			}
		} elseif ($options['type'] == 'crop') {
			if ($w > $options['width']) {
				$this->crop('w', $options['width'], $options['height']);
			} elseif ($h > $options['height']) {
				$this->crop('h', $options['width'], $options['height']);
			}
		}

		return $value;
	}

	function getImageResource() {
		if (!$this->_resource) {
			$func = '';
			//$mimeType = mime_content_type($this->_file->getRealPath());
			$mimeType = 'image/jpeg';
			switch ($mimeType) {
				case 'image/gif':
					$func = 'imagecreatefromgif';
					$this->_resourceType = 'gif';
				break;

				case 'image/jpeg':
					$func = 'imagecreatefromjpeg';
					$this->_resourceType = 'jpeg';
				break;

				case 'image/png':
					$func = 'imagecreatefrompng';
					$this->_resourceType = 'png';
				break;
			}
			if (!$func || !$this->_resource = $func($this->_file->getRealPath())) {
				trigger_error('Image create error', E_USER_ERROR);
			}
		}
		return $this->_resource;
	}

	function getWidth() {
		$img = $this->getImageResource();
		return imagesx($img);
	}

	function getHeight() {
		$img = $this->getImageResource();
		return imagesy($img);
	}

	function zoom($type, $size) {

		$img = $this->getImageResource();

		$w = imagesx($img);
		$h = imagesy($img);

		$newW = ($type == "h") ? floor(($w * $size) / $h) : $size;
		$newH = ($type == "w") ? floor(($h * $size) / $w) : $size;

		$x = 0;
		$y = 0;

		$newImg = imagecreatetruecolor($newW, $newH);
		$result = imagecopyresampled($newImg, $img, 0, 0, $x, $y, $newW, $newH, $w, $h);

		if ($result) {
			$this->_resource = $newImg;
			if ($this->_resourceType !== null) {
				$func = 'image'.$this->_resourceType;
				if ($this->_resourceType == 'jpeg') {
					return $func($newImg, $this->_file->getRealPath(), 100);
				} else {
					return $func($newImg, $this->_file->getRealPath());
				}
			}
		}

		trigger_error('Image resize error', E_USER_ERROR);
	}

	function crop($type, $newW, $newH) {

		$img = $this->getImageResource();

		$w = imagesx($img);
		$h = imagesy($img);

		$kw = $w - (($newW * $h) / $newH);
		$kh = $h - (($newH * $w) / $newW);

		$x1 = 0;
		$x2 = $w;

		$y1 = 0;
		$y2 = $h;

		if ($kw > 0) {
			$x1 += $kw / 2;
			$x2 -= $kw / 2;
		}

		if ($kh > 0) {
			$y1 += $kh / 2;
			$y2 -= $kh / 2;
		}

		$newImg = imagecreatetruecolor($x2 - $x1, $y2 - $y1);
		$result = imagecopy($newImg, $img, 0, 0, $x1, $y1, $x2, $y2);
		if ($result) {
			$this->_resource = $newImg;
			if ($type == "w") {
				return $this->zoom("w", $newW);
			} else if ($type == "h") {
				return $this->zoom("h", $newH);
			}
		}

		trigger_error('Image resize error', E_USER_ERROR);
	}

}

?>